<?php
/**
 * JoosFish - Multi Lingual extention and translation manager for Joostina CMS
 * Copyright (C) 2011 Joostina Team
 * 
 * @name JoosFish
 * @copyright © 2011 Joostina team, © 2003-2007 Think Network GmbH, Munich
 * @license GNU General Public License version 2
 * @version 1.8.2.1
 * @package Joostina
 *
 * Based on the source code of Joom!Fish 1.8.2 copyrighted by Think
 * Network GmbH, Munich. The main contribution is made by developers of
 * Joom!Fish project, but the main reason behind making JoosFish an alongside
 * project is to maintain it's compatibility with Joostina CMS 1.3.x
 * 
 * For full copyright note see documetation/COPYRIGHT.php
 * 
 * For changes see documentation/CHANGELOG.php
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * -----------------------------------------------------------------------------
 * $Id: jfalternative.php 568 2007-07-17 06:29:53Z akede $
 * @package joomfish
 * @subpackage contentbot.alternative_lang
 *
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$_MAMBOTS->registerFunction( 'onPrepareContent', 'botJoomfishAlternative' );

function botJoomfishAlternative( $published, &$row, &$params, $page=0 ) {

	// simple performance check to determine whether bot should process further
	if ( strpos( $row->text, 'jfalternative' ) === false ) {
		return true;
	}

	// define the regular expression for the bot
	$regex = "#{jfalternative}(.*?){/jfalternative}#s";

	// check whether mambot has been unpublished
	if ( !$published ) {
		$row->text = preg_replace( $regex, '', $row->text );
		return true;
	}

	preg_match($regex, $row->text, $matches);
	if (count($matches)!=2) return true;

	list($id,$table,$defaultText) = explode("|",$matches[1]);

	// only support tables at this time.
	if ($table!="content"){
		$row->text = preg_replace( $regex, '', $row->text );
		return true;
	}

	global $database,$mosConfig_live_site;
	$sql = "SELECT DISTINCT jfl.shortcode, jfl.iso, jfl.name, jfl.image FROM #__jf_content AS jfc, #__languages AS jfl "
	."\n WHERE reference_id=$id AND reference_table='".$table."'"
	."\n  AND jfc.language_id=jfl.id AND jfc.published=1";
	$database->setQuery($sql);
	$alts = $database->loadObjectList();

	if (is_array($alts) && count($alts)>0){
		$alttext = "<div class='jf_altlanguages'>";
		$alttext .=  JText::_('NO TRANSLATION ALTERNATIVE');
		foreach ($alts as $lang) {
			$lang->shortcode = ($lang->shortcode == '') ? $lang->iso : $lang->shortcode;
			$alttext .="&nbsp;<a href='".sefRelToAbs("index.php?option=com_content&task=view&id=$id&lang=".$lang->shortcode)."'>";
			if ($params->get("falt_showAS","flag")=="flag"){
				if( isset($lang->image) && $lang->image!="" ) {
					$langImg = '/images/' .$lang->image;
				} else {
					$langImg = '/components/com_joomfish/images/flags/' .$lang->shortcode .".gif";
				}
				$langImg ='<img src="' .$mosConfig_live_site . $langImg. '" alt="' .$lang->name. '" title="' .$lang->name. '" border="0" />';
				$alttext .=$langImg."</a>&nbsp;";
			}
			else {
				$alttext .=$lang->name."</a>";
			}
		}
		$alttext .= "</div>";
		$row->text = preg_replace( $regex, $alttext, $row->text );
	}
	else {
		global $_JOOMFISH_MANAGER;
		$alttext = "<div class='jf_altlanguages'>";
		$alttext .= JText::_('NO TRANSLATION AVAILABLE');
		$alttext .= "</div>";
		$row->text = preg_replace( $regex, $alttext, $row->text );
	}

	return true;
}
?>